package com.quectel.util.common;

import lombok.extern.slf4j.Slf4j;

import java.net.URL;
import java.util.Objects;

/**
 * 连接帮助类
 *
 * @author: caolu
 * @email: louis.cao@quectel.com
 * @date: 2022-11-28 09:42:00
 */
@Slf4j
public class URLUtils {


    /**
     * @param url http://192.168.1.1:80/
     * @return
     */
    public static String getHost(String url) {
        try {
            URL urlObj = new URL(url);
            return urlObj.getHost();
        } catch (Exception e) {
            log.warn("Parse url failed");
        }
        return null;
    }

    /**
     * @param url        http://192.168.1.1:80/
     * @param anotherUrl
     * @return
     */
    public static boolean eqHost(String url, String anotherUrl) {

        try {
            URL urlObj = new URL(url);
            URL anotherUrlObj = new URL(anotherUrl);
            if (urlObj.getHost() != null && Objects.equals(urlObj.getHost(), anotherUrlObj.getHost())) {
                return true;
            }
        } catch (Exception e) {
            log.warn("Parse url failed");
        }
        return false;
    }
}
